package de.h_da.fbi.cg.pointcloud.spatial;

import java.util.Arrays;

/**
 * @author Nicolas Gramlich
 * @since 19:27:30 - 11.11.2010
 * @param <C>
 */
public class Point<C extends Comparable<C>> implements IPoint<C> {
	// ===========================================================
	// Constants
	// ===========================================================

	// ===========================================================
	// Fields
	// ===========================================================

	private Comparable<C>[] mCoordinates;

	// ===========================================================
	// Constructors
	// ===========================================================

	@SuppressWarnings("unchecked")
	public Point(final int pDimensions) {
		if(pDimensions <= 0) {
			throw new IllegalArgumentException();
		}
		this.mCoordinates = new Comparable[pDimensions];
	}

	public Point(final C pX, final C pY) {
		this(2);
		this.setCoordinate(0, pX);
		this.setCoordinate(1, pY);
	}

	public Point(final C pX, final C pY, final C pZ) {
		this(3);
		this.setCoordinate(0, pX);
		this.setCoordinate(1, pY);
		this.setCoordinate(2, pZ);
	}

	// ===========================================================
	// Getter & Setter
	// ===========================================================

	public void setCoordinate(final int pDimension, final C pValue) throws ArrayIndexOutOfBoundsException {
		this.mCoordinates[pDimension] = pValue;
	}

	@Override
	@SuppressWarnings("unchecked")
	public C getCoordinate(final int pDimension) {
		return (C) this.mCoordinates[pDimension];
	}

	@Override
	public int getDimensions() {
		return this.mCoordinates.length;
	}

	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	// ===========================================================

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + Arrays.hashCode(this.mCoordinates);
		return result;
	}

	@Override
	public boolean equals(final Object pOther) {
		if(this == pOther) {
			return true;
		}
		if(pOther == null) {
			return false;
		}
		if(this.getClass() != pOther.getClass()) {
			return false;
		}
		final Point<?> otherGenericPoint = (Point<?>) pOther;
		if(!Arrays.equals(this.mCoordinates, otherGenericPoint.mCoordinates)) {
			return false;
		}
		return true;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Object clone() {
		final Point<C> genericPoint = new Point<C>(this.mCoordinates.length);
		for(int i = 0; i < this.mCoordinates.length; ++i) {
			genericPoint.setCoordinate(i, (C) this.mCoordinates[i]);
		}
		return genericPoint;
	}

	@Override
	public String toString() {
		return Arrays.toString(this.mCoordinates);
	}

	// ===========================================================
	// Methods
	// ===========================================================

	// ===========================================================
	// Inner and Anonymous Classes
	// ===========================================================
}
